local skel = fk.CreateSkill{
  name = "kqf__tuobi",
}

Fk:loadTranslationTable{
  ["kqf__tuobi"] = "托庇",
  [":kqf__tuobi"] = "每轮开始时，你可令两到三名角色两两进行一次拼点，赢者摸一张牌，再赢则受到1点火焰伤害，"..
  "输者横置，再输则摸三张牌；最后，由你执行本次没有角色执行过的效果。",

  ["#kqf__tuobi_choose"] = "托庇：你可以令两到三名角色两两进行一次拼点，赢者摸一张牌，再赢则受到1点火焰伤害，"..
  "输者横置，再输则摸三张牌；最后，由你执行本次没有角色执行过的效果。",
  ["@@kqf__tuobi_win-turn"] = "托庇-赢",
  ["@@kqf__tuobi_lose-turn"] = "托庇-输",

  ["$kqf__tuobi1"] = "弱者，就不该存在于这个世界。",
  ["$kqf__tuobi2"] = "逃跑，或者是死。",
}

---@param p1 ServerPlayer
---@param p2 ServerPlayer
---@param player ServerPlayer
---@param aaa integer[]
---@return integer[]
local DoPindianOnce = function(p1,p2,player,aaa)
  local room = player.room
  if p1.dead or p2.dead or (not p1:canPindian(p2)) then return aaa end
  local pindian = p1:pindian({p2}, skel.name)
  local winner = pindian.results[p2].winner
  if not winner then return aaa end

  local list = {p1,p2}--排序两人，下面的内容赢家先执行效果
  if list[2] == winner then
    table.remove(list,2)
    table.insert(list,1,winner)
  end

  for _, p in ipairs(list) do
    if winner == p then
      --赢了
      if p:getMark("@@kqf__tuobi_win-turn") > 0 then
        --再赢
        room:damage{
        from = player,
        to = p,
        damage = 1,
        damageType = fk.FireDamage,
        skillName = skel.name,
        }
        table.insertIfNeed(aaa,"w2")
      else
        --第一次赢
        p:drawCards(1,skel.name)
        table.insertIfNeed(aaa,"w1")
      end
      room:setPlayerMark(p, "@@kqf__tuobi_win-turn", 1)
    else
      --没赢
      if p:getMark("@@kqf__tuobi_lose-turn") > 0 then
        --再输
        p:drawCards(3,skel.name)
        table.insertIfNeed(aaa,"l2")
      else
        --第一次输
        p:setChainState(true)
        table.insertIfNeed(aaa,"l1")
      end
      room:setPlayerMark(p, "@@kqf__tuobi_lose-turn", 1)
    end
  end
return aaa
end

skel:addEffect(fk.RoundStart, {
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return end
    for _, p in ipairs(player.room:getAlivePlayers()) do
    for _, pp in ipairs(player.room:getAlivePlayers()) do
      if p:canPindian(pp) then return true end
    end end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets ={}
    for _, p in ipairs(player.room:getAlivePlayers()) do
    for _, pp in ipairs(player.room:getAlivePlayers()) do
      if p:canPindian(pp) then
        table.insertIfNeed(targets, pp)
      end 
    end end
    local to = room:askToChoosePlayers(player, {
      min_num = 2,
      max_num = 3,
      targets = targets,
      skill_name = skel.name,
      prompt = "#kqf__tuobi_choose",
      cancelable = true,
    })
    if #to > 1 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self).tos
    room:sortByAction(targets)
    --
    local aaa = {}
    aaa = DoPindianOnce(targets[1],targets[2],player,aaa)
    if #targets >2 then
      aaa = DoPindianOnce(targets[1],targets[3],player,aaa)

      aaa = DoPindianOnce(targets[2],targets[3],player,aaa)
    end
    --执行没执行过的效果
    if not table.contains(aaa,"w1") then
      player:drawCards(1,skel.name)
    end
    if not table.contains(aaa,"w2") then
      room:damage{
        from = player,
        to = player,
        damage = 1,
        damageType = fk.FireDamage,
        skillName = skel.name,
      }
    end
    if not table.contains(aaa,"l1") then
      player:setChainState(true)
    end
    if not table.contains(aaa,"l2") then
      player:drawCards(3,skel.name)
    end
    --所有人清理标记
    for _, p in ipairs(targets) do
      room:setPlayerMark(p, "@@kqf__tuobi_win-turn", 0)
      room:setPlayerMark(p, "@@kqf__tuobi_lose-turn", 0)
    end

  end
})


return skel